package com.lottery.service.qiuqiu.statisticsService;

import com.lottery.dao.qiuqiu.lobbyDao.LotteryDao;
import com.lottery.dao.qiuqiu.statisticsDao.AgencyDao;
import com.lottery.dao.qiuqiu.statisticsDao.AgencyMoneyDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.Agency;
import com.lottery.entity.AgencyMoney;
import com.lottery.service.qiuqiu.wealthService.uitil.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class AgencyMoneyService {

	@Resource
	private AgencyMoneyDao agencyMoneyDao;

	@Resource
	private LotteryDao lotteryDao;

	@Resource
	private AgencyDao agencyDao;

	/**
	 * 插入一条数据
	 */
	public ResultDto<AgencyMoney> insetAgencyMoney(String expect) {
		ResultDto<AgencyMoney> resultDto = new ResultDto<AgencyMoney>();
		AgencyMoney agencyMoney = null;
		List<AgencyMoney> list = agencyMoneyDao.getAgencyMoneyLimit(expect);
		if (list != null && list.size() > 0) {
			for (AgencyMoney agencyMoneylist : list) {
				agencyMoney = new AgencyMoney();
				// 投注总金额
				double bettingmoney = agencyMoneylist.getBettingMoney();
				// 中奖总金额
				double winTotalmoney = agencyMoneylist.getWinTotalmoney();
				// 收益
				double income = 0;
				AgencyMoney am = new AgencyMoney();
				am.setExpect(expect);
				am.setAgencyId(agencyMoneylist.getAgencyId());
				double bettingmoney2 = 0;
				bettingmoney2 = agencyMoneyDao.getBettingmoney(am);
				double winningamount2 = 0;
				winningamount2 = agencyMoneyDao.getWinningamount(am);
				income = bettingmoney2 - winningamount2;
				DecimalFormat df = new DecimalFormat("#0.##");
				df.setRoundingMode(RoundingMode.FLOOR);
				income = Double.valueOf(df.format(income));
				// 代理方id
				String agencyId = agencyMoneylist.getAgencyId();
				agencyMoney.setAgencyDate(new Date());
				agencyMoney.setAgencyId(agencyId);
				agencyMoney.setBettingMoney(bettingmoney);
				agencyMoney.setIncome(income);
				agencyMoney.setWinTotalmoney(winTotalmoney);
				agencyMoney.setExpect(expect);
				try {
					agencyMoneyDao.insertAgencyMoney(agencyMoney);
					resultDto.setMessage("200");
					resultDto.setSuccess(true);
				} catch (Exception e) {
					resultDto.setMessage("400" + e.toString());
					resultDto.setSuccess(false);
					return resultDto;
				}
			}
		}
		return resultDto;
	}

	/**
	 * 推广渠道商统计接口
	 */

	public ResultDto<List<Map<String, String>>> getAgencyMoney() {
		ResultDto<List<Map<String, String>>> resultDto = new ResultDto<List<Map<String, String>>>();
		// 结果list
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		// 查询所有代理方id
		List<Agency> agencyList = agencyDao.selectAllAgency();
		// 期号降序排列最新5期的记录
		List<AgencyMoney> expectList = agencyMoneyDao.selectAgencyMoneyExpect();

		Map<String, String> map = new HashMap<String, String>();
		if (expectList == null || expectList.size() == 0) {
			map.put("date", "0");
			list.add(map);
			resultDto.setList(list);
			resultDto.setSuccess(true);
		} else {
			// 反转
			Collections.reverse(expectList);
			try {
				for (int i = 0; i < expectList.size(); i++) {

					// 期号
					String expect = expectList.get(i).getExpect();
					// 获取当前日期的开奖时间
					Date opentime = lotteryDao.selectLotteryByExpect(expect).getOpentime();
					String date = DateUtil.formatDate(opentime, DateUtil.YMDHM);
					map.put("date", date);// 保存时间
					for (Agency agency : agencyList) {
						AgencyMoney agencyMoney = new AgencyMoney();
						agencyMoney.setExpect(expect);
						agencyMoney.setAgencyId(agency.getId());
						double agencyMoney1Money = 0.0;// 代理id投注总金额
						AgencyMoney agencyMoney1 = agencyMoneyDao.getAgencyMoney(agencyMoney);
						if (agencyMoney1 != null) {
							agencyMoney1Money = agencyMoney1.getBettingMoney();// 设置投注总金额
						}
						map.put(agency.getAgencyName(), Double.toString(agencyMoney1Money));// 保存
					}
					list.add(map);
				}
				resultDto.setList(list);
				resultDto.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
				resultDto.setSuccess(false);
			}
		}
		return resultDto;
	}

}
